import { configureStore } from "@reduxjs/toolkit"
import { combineReducers } from "redux"
import { persistStore, persistReducer } from "redux-persist"
import { FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER } from "redux-persist/es/constants"
import storage from "redux-persist/lib/storage"
import authReducer from "./auth/slice"
import lanReducer from "./language/slice"

const persistConfig = {
  key: "root",
  storage,
  whitelist: ["auth", "lan"],
}

const rootReducer = combineReducers({
  auth: authReducer,
  lan: lanReducer,
})

const persistedReducer = persistReducer(persistConfig, rootReducer)

const store = configureStore({
  reducer: persistedReducer,
  middleware: (getDefaultMiddleware) => [
    ...getDefaultMiddleware({
      serializableCheck: {
        ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
      },
    }),
  ],
  devTools: true,
})

export type RootState = ReturnType<typeof store.getState>

const persistor = persistStore(store)

export default { store, persistor }
